package com.zxbg.controller.systemtoolsController;

import com.zxbg.controller.Code;
import com.zxbg.controller.Result;
import com.zxbg.domain.NavMenu;
import com.zxbg.service.systemtoolsService.MenuManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/MenuManage")
public class MenuManageController {
    @Autowired
    MenuManageService menuManageService;

    @PostMapping
    public Result getAllNavMenu(@RequestBody Map<String, Object> search_info, @RequestHeader Integer tenant_id) {
        List<NavMenu> navMenuList = menuManageService.getAllNavMenu(tenant_id);
        return getPageList(navMenuList, search_info);
    }

    @PostMapping("/addNavMenu")
    public Result addNavMenu(@RequestBody NavMenu navMenu, @RequestHeader Integer tenant_id, @RequestHeader String employee_id) {
        navMenu.setCreate_time(new Date());
        navMenu.setMenu_name_zh(navMenu.getMenu_name().getZh());
        navMenu.setMenu_name_en(navMenu.getMenu_name().getEn());
        navMenu.setTenant_id(tenant_id);
        navMenu.setCreator_id(employee_id);
        return new Result(Code.SAVE_OK, menuManageService.addNavMenu(navMenu));
    }

    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody Map<String, Integer> map) {
        return new Result(Code.UPDATE_OK, menuManageService.changeStatus(map.get("menu_id"), map.get("status")));
    }

    @PutMapping
    public Result editNavMenu(@RequestBody NavMenu navMenu) {
        navMenu.setMenu_name_zh(navMenu.getMenu_name().getZh());
        navMenu.setMenu_name_en(navMenu.getMenu_name().getEn());
        return new Result(Code.UPDATE_OK, menuManageService.editNavMenu(navMenu));
    }

    @DeleteMapping("/{menu_id}")
    public Result deleteNavMenu(@PathVariable Integer menu_id) {
        return new Result(Code.DELETE_OK, menuManageService.deleteNavMenu(menu_id));
    }

    @PostMapping("/{key}")
    public Result getNavMenuByKey(@PathVariable String key, @RequestBody Map<String, Object> search_info, @RequestHeader Integer tenant_id) {
        List<NavMenu> navMenuList = menuManageService.getNavMenuByKey((String) search_info.get("key"), tenant_id);
        return getPageList(navMenuList, search_info);
    }

    public Result getPageList(List<NavMenu> navMenuList, Map<String, Object> search_info) {
        List<NavMenu> resList = new ArrayList<>();
        Integer startNum = ((Integer)search_info.get("current_page") - 1) * (Integer)search_info.get("page_size");
        Integer endNum = (Integer)search_info.get("current_page") * (Integer)search_info.get("page_size") - 1;
        for (int i = 0; i < navMenuList.size(); i++) {
            if(i >= startNum && i <= endNum) {
                resList.add(navMenuList.get(i));
            }
        }
        Map<String,Object> first_map = new HashMap<>();
        Map<String,Object> second_map = new HashMap<>();
        second_map.put("page_size", search_info.get("page_size"));
        second_map.put("current_page", search_info.get("current_page"));
        second_map.put("total", navMenuList.size());
        first_map.put("page", second_map);
        first_map.put("rows", resList);
        return new Result(Code.GET_OK, first_map);
    }
}
